// 存储学生列表数据
let tableData = [];

// 获取学生列表数据
async function getTableData() {
    let result = await axios({
        url: '/searchStudent',
        method: 'get',
    });
    result = result.data;
    tableData = result;
    renderTable(tableData);
}

// 渲染学生列表
function renderTable(data) {
    let str = '';
    data.forEach((item, index) => {
        str += `<tr>
        <td>${item.saccount}</td>
        <td>${item.sname}</td>
        <td>${item.classid}</td>
        <td>${item.chinese}</td>
        <td>${item.math}</td>
        <td>${item.english}</td>
        <td>
            <button class="btn btn-success edit" data-toggle="modal" data-target="#editMessage">编辑</button>
        </td>
        </tr>
        `
    });
    $('#table-body').html(str);
}

// 通过学号获取学生id
async function getID(saccount){
    const result = await axios({
        method:'post',
        url:'/searchIdByAccount',
        data:{
            saccount
        }
    });
    if (result.data.msg) {
        alert(result.data.msg);
    }else{
        return result.data[0].sid;
    }
}

// 获取学生列表数据
getTableData();

// 修改学生成绩
let updateForm = $("#update-student-form")[0];
updateForm.onclick =async function(e){
    if (e.target.id == 'update-student-btn') {
        let saccount = $('#sNo1').val();
        let sid = await getID(saccount);
        let chineseS = +$('#chinese1').val();
        let mathS = +$('#math1').val();
        let englishS = +$('#english1').val();
        await axios({
            method:'post',
            url:'/deleteScore',
            data:{
                sid
            }
        });
        axios({
            method:'post',
            url:'/insertScore',
            data:{
                sid,
                chineseS,
                mathS,
                englishS
            }
        }).then(resp => {
            alert(resp.data.msg);
        })
    }
}

// 模态框修改学生成绩
let tableBody = $('#table-body')[0];
tableBody.onclick = async function(e){
    if (e.target.className == 'btn btn-success edit') {
        let saccount = e.target.parentNode.parentNode.firstElementChild.textContent;
        let sid = await getID(saccount);
        let editBtn = $('#editSubmitBtn');
        editBtn.on('click',async function(e) {
            let chineseS = $('#chinese').val();
            let mathS = $('#math').val();
            let englishS = $('#english').val();
            let isTrue = confirm('确认要修改吗？');
            if (isTrue) {
                await axios({
                    method:'post',
                    url:'/deleteScore',
                    data:{
                        sid
                    }
                });
                axios({
                    method:'post',
                    url:'/insertScore',
                    data:{
                        sid,
                        chineseS,
                        mathS,
                        englishS
                    }
                }).then(resp => {
                    alert(resp.data.msg);
                })
            }
        })
    }
}

// 退出登录
let backTo = $('#back');
backTo.on('click', e=>{
    let isTrue = confirm('确定要退出吗？');
    if (isTrue) {
        location.href = '/';
    }
})
